<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Notification extends Controller_Initial_Page {
   
    public function action_internal_index()
    {  
        $user_model = Auth::instance()->get_user(); 
        
        $notification_model = ORM::factory('notification');
        $notifications = $notification_model->get_notifications($user_model->id,15);
       
        $notification_view = View::factory('notification/index');
        $notification_view->set('notifications',$notifications);
        
        $this->response->body($notification_view);
        
    }
    
    public function action_ajax_get_toggle()
    {
        $user_model = Auth::instance()->get_user();
        
        $notification_model = ORM::factory('notification');
        $notifications = $notification_model->get_notifications($user_model->id, 5, false);    
    
        //update user notice table
        foreach($notifications as $notification)
        {
            $notice_model = $user_model->notice;
            $notice_model->notification_id = $notification['id'];
            $notice_model->save();
            break;
        }
        
        $notifiation_toggle_template_view = View::factory('notification/toggle_template');
        $notifiation_toggle_template_view->set('notifications',$notifications);
        
        $content = array('content' =>  $notifiation_toggle_template_view->render());
        $this->response->body(json_encode($content));
    }
}